package net.baisoft.basessh.dao.hibernate;

import java.util.List;

import net.baisoft.basessh.dao.AdminDao;
import net.baisoft.basessh.model.Admin;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AdminDaoHibernate extends HibernateDaoSupport implements AdminDao {

	@Override
	public void add(Admin admin) {
		getHibernateTemplate().save(admin);
	}

	@Override
	public void delete(int id) {
		getHibernateTemplate().delete(get(id));
	}

	@Override
	public void update(Admin admin) {
		getHibernateTemplate().update(admin);
	}

	@Override
	public Admin get(int id) {
		return (Admin)getHibernateTemplate().get(Admin.class, id);
	}

	@Override
	public Admin get(String username, String password) {
		String hql = "from Admin admin where admin.username='" + username + "' and admin.password='" + password + "'";
		
		@SuppressWarnings("unchecked")
		List<Admin> admins = getHibernateTemplate().find(hql);
		if(admins.isEmpty()){
			return null;
		} else {
			return admins.get(0);
		}
	}

}
